package net.yunxinyong.cec.controller;

import net.yunxinyong.cec.entity.Cec702Entity;
import net.yunxinyong.cec.service.Cec702Service;
import net.yunxinyong.cec.utils.CECResult;
import net.yunxinyong.cec.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**
 *@author  李海生
 *@date  2018/9/20
 */
@RestController
@RequestMapping("702")
public class Cec702Controller {

    @Autowired
    private Cec702Service cec702Service;

    /*
     * 查询所有企业信息
     * */
    @RequestMapping(value = "select", method = RequestMethod.POST)
    public CECResult findAll(int page, int rows, Cec702Entity cec702) {
        PageBean pageBean = cec702Service.findAll(page, rows, cec702);
        if (pageBean.getPageDatas().isEmpty()) {
            return CECResult.build(200, "没有查询到任何企业", pageBean);
        } else {
            return CECResult.build(200, "查询成功!", pageBean);
        }
    }

    /*
     * 根据id更新企业信息
     * */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CECResult updateById(int id, int status) {
        int count = cec702Service.updateById(id, status);
        if (count != 0) {
            return CECResult.build(200, "更新成功!", count);
        }
        return CECResult.build(200, "更新失败!", count);
    }

    /*
     * 根据id删除数据
     * */
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public CECResult deleteById(@RequestParam("id") int id) {
        int count = cec702Service.deleteById(id);
        if (count != 0) {
            return CECResult.build(200, "删除成功!", count);
        }
        return CECResult.build(200, "删除失败!", count);
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public CECResult insertOneByBaseId(Cec702Entity cec702) {
//        System.out.println("JSON数据:" + JSONObject.fromObject(cecOne));
        //调用findOneBybaseId方法，返回的list大小如果为0说明该企业没有注册
        List<Cec702Entity> list = cec702Service.findOneByBaseId(cec702.getBaseId());
        if (list.size() != 0) {
            return CECResult.build(200, "该企业已经存在!", list);
        }
        int count = cec702Service.insertOne(cec702.getBaseId());
        return CECResult.build(200, "注册成功!", list);
    }
}
